/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nokia.iap.ui.lib.onebutton;

import com.nokia.mid.payment.IAPClientPaymentManager;

/**
 *
 * @author zinin
 */
public class GxButtonStart extends GxButtonBase{
    private final OneButtonIAP context;

    private boolean isInprogress;
    private final String KCancelLabel = "Cancel";
    private final String KStartLabel = "Start";
    
    public GxButtonStart(OneButtonIAP client, GxColorTheme theme){
        super("Start", 0, 0, theme);
        context = client;
    }

    public void performAction() {
        if(isInprogress){
            cancelCurrentOperation();
        }else{
            context.screenBuf.delete(0, context.screenBuf.length());
            context.screenBuf.append(context.KHorzCenterMark + "Retrieving\n");
            context.screenBuf.append(context.KHorzCenterMark + "product information\n");
            context.screenBuf.append(context.KHorzCenterMark + "..please wait..");
            int errcode = context.iapClient.getProductData(OneButtonIAP.productID);
            if(errcode == IAPClientPaymentManager.SUCCESS){
                drawer.setLabel(KCancelLabel);
                isInprogress = true;
                context.masterBar.advance();
            }else{
                context.screenBuf.delete(0, context.screenBuf.length());
                context.screenBuf.append(context.KHorzCenterMark + "ERROR : ").append(String.valueOf(errcode)).append("\n");
                isInprogress = false;
            }
        }
    }

    private void cancelCurrentOperation() {
        context.screenBuf.delete(0, context.screenBuf.length());
        context.screenBuf.append(context.KHorzCenterMark + "..canceling..");
        context.cancelProcess();
    }

    public boolean isHidden() {
        return (!isVisible());
    }
    
    public void setToInitialState()
    {
        drawer.setLabel(KStartLabel);
        isInprogress = false;
    }
}
